package com.idp.app.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.idp.app.dto.*;
import com.idp.app.excel.SyAjxxExcelDto;
import com.idp.app.mapper.SyAjxxMapper;
import com.idp.app.service.SyAjxxService;
import com.idp.app.util.ExcelExportUtils;
import com.idp.common.base.BaseController;
import com.idp.common.model.PageModel;
import com.idp.common.model.ResultModel;
import com.idp.common.utils.DateUtils;
import com.idp.common.utils.ExcelExportUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * 用户表Controller
 *
 * @author Simon
 */
@Api(tags = "案件服务")
@RestController
@RequestMapping("/syAjxx/")
public class SyAjxxController extends BaseController {

    @Autowired
    private SyAjxxService syAjxxService;

    /**
     * 分页查询
     *
     * @param syAjxxSearchDto
     * @return
     */
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页数", paramType = "query", required = true, defaultValue = "1"), @ApiImplicitParam(name = "rows", value = "每页行数", paramType = "query", required = true, defaultValue = "10")})
    @PostMapping("findByPage")
    public ResultModel<PageModel<SyAjxxDto>> findByPage(@RequestBody SyAjxxSearchParamDto syAjxxSearchDto) {
        return ResultModel.success(syAjxxService.findByPage(syAjxxSearchDto));
    }


    /**
     * 查询该地区下所有的案件
     *
     * @param syAjxxSearchDto
     * @return
     */
    @ApiOperation(value = "查询该地区下所有的案件-入参只需要传dqbm和lx即可")
    @PostMapping("findAjByPage")
    public ResultModel<List<SyAjxxDto>> findAjByPage(@RequestBody SyAjxxSearchDto syAjxxSearchDto) {
        return ResultModel.success(syAjxxService.findAjByPage(syAjxxSearchDto));
    }

    /**
     * 通过id查询
     *
     * @param syAjxxDto
     * @return
     */
    @ApiOperation(value = "通过id查询")
    @ApiOperationSupport(includeParameters = {"syAjxxDto.id"})
    @PostMapping("getById")
    public ResultModel<SyAjxxDto> getById(@RequestBody SyAjxxDto syAjxxDto) {
        return ResultModel.success(syAjxxService.getById(syAjxxDto.getId()));
    }

    /**
     * 新增
     *
     * @param syAjxxDto
     * @return
     */
    @ApiOperation(value = "新增")
    @ApiOperationSupport(ignoreParameters = {"syAjxxDto.id"})
    @PostMapping("add")
    public ResultModel add(@RequestBody SyAjxxDto syAjxxDto) {
        syAjxxService.add(syAjxxDto);
        return ResultModel.success("新增成功", null);
    }

    /**
     * 修改
     *
     * @param syAjxxDto
     * @return
     */
    @ApiOperation(value = "修改")
    @PostMapping("update")
    public ResultModel update(@RequestBody SyAjxxDto syAjxxDto) {
        syAjxxService.update(syAjxxDto);
        return ResultModel.success("修改成功", null);
    }

    /**
     * 删除
     *
     * @param syAjxxDto
     * @return
     */
    @ApiOperation(value = "删除")
    @ApiOperationSupport(includeParameters = {"syAjxxDto.id"})
    @PostMapping("delete")
    public ResultModel delete(@RequestBody SyAjxxDto syAjxxDto) {
        syAjxxService.delete(syAjxxDto.getId());
        return ResultModel.success("删除成功", null);
    }

    /**
     * 案件信息导入
     *
     * @param file
     * @return
     */
    @ApiOperation("案件信息导入")
    @PostMapping("importExcel")
    public ResultModel importExcel(MultipartFile file) {
        syAjxxService.importExcel(file);
        return ResultModel.success("导入成功", null);
    }

    /**
     * 案件导入之下载模版
     *
     * @return
     */
    @ApiOperation("案件导入之下载模版")
    @GetMapping("downloadExcel")
    public ResponseEntity<byte[]> downloadExcel() {
        try {
            // Load the template file from resources
            ClassPathResource classPathResource = new ClassPathResource("templates/案件导入模版.xlsx");
            InputStream inputStream = classPathResource.getInputStream();
            byte[] templateBytes = toByteArray(inputStream);
            inputStream.close();

            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=案件导入模版.xlsx");
            headers.add(HttpHeaders.CONTENT_TYPE, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

            return new ResponseEntity<>(templateBytes, headers, HttpStatus.OK);
        } catch (IOException e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    private byte[] toByteArray(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) != -1) {
            byteArrayOutputStream.write(buffer, 0, length);
        }
        return byteArrayOutputStream.toByteArray();
    }

    /**
     * 用户信息导出
     *
     * @return
     */
    @ApiOperation("案件信息导出")
    @GetMapping("exportExcel")
    public void exportExcel(SyAjxxSearchExportDto syAjxxDto, HttpServletResponse response) throws IOException {
        try {
            List<SyAjxxExportDto> list = syAjxxService.getListInfo1(syAjxxDto);
            ExcelExportUtils.exportExcel(response, list);
        } catch (Exception e) {
            throw new RuntimeException("导出失败");
        }
    }


}
